package com.xuchang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuchang.domain.SysMenu;
import com.xuchang.domain.SysPrivilege;
import com.xuchang.domain.SysRoleMenu;
import com.xuchang.domain.SysRolePrivilege;
import com.xuchang.mapper.SysMenuMapper;
import com.xuchang.model.RolePrivilegesParam;
import com.xuchang.model.SysMenuPrivilegesDto;
import com.xuchang.service.SysMenuService;
import com.xuchang.service.SysPrivilegeService;
import com.xuchang.service.SysRoleMenuService;
import com.xuchang.service.SysRolePrivilegeService;
import com.xuchang.mapper.SysRolePrivilegeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author xuchang
* @description 针对表【sys_role_privilege(角色权限配置)】的数据库操作Service实现
* @createDate 2025-06-29 22:19:07
*/
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege>
    implements SysRolePrivilegeService{
    @Autowired
    private SysPrivilegeService sysPrivilegeService;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public List<SysMenuPrivilegesDto> findSysMenuAndPrivileges(Long roleId) {
        List<SysMenu> list = sysMenuService.list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 页面显示的是二级菜单,以及二级菜单包含的权限
        List<SysMenu> rootMenus = list.stream().filter(item -> item.getParentId() == null).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(rootMenus)){
            return Collections.emptyList();
        }
        // 查询所有的二级菜
        ArrayList<SysMenuPrivilegesDto> menuPrivilegesDtoArrayList = new ArrayList<SysMenuPrivilegesDto>();
        for (SysMenu rootMenu : rootMenus) {
            menuPrivilegesDtoArrayList.addAll(getChildMenus(rootMenu.getId(),roleId,list));
        }

        return menuPrivilegesDtoArrayList;
    }

    /**
     * 查询菜单的子菜单 (递归)
     *
     * @param parentId 父菜单的ID
     * @param roleId   当前查询的角色的ID
     * @return
     */
    private List<SysMenuPrivilegesDto> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        ArrayList<SysMenuPrivilegesDto> sysMenuPrivilegesDtos = new ArrayList<>();
        for (SysMenu sysMenu : sources) {
            if(sysMenu.getParentId()== parentId){
                SysMenuPrivilegesDto sysMenuPrivilegesDto = new SysMenuPrivilegesDto();
                BeanUtils.copyProperties(sysMenu,sysMenuPrivilegesDto);
                sysMenuPrivilegesDto.setChildMenus(getChildMenus(sysMenu.getId(), roleId, sources));
                List<SysPrivilege> privilegeByMenu = sysPrivilegeService.findPrivilegeByMenu(sysMenu.getId(), roleId);
                sysMenuPrivilegesDto.setPrivileges(privilegeByMenu);
                sysMenuPrivilegesDtos.add(sysMenuPrivilegesDto);
            }
        }
        return sysMenuPrivilegesDtos;
    }


    @Override
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        Long roleId = rolePrivilegesParam.getRoleId();
        // 先删除之前该角色的权限
        remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId,roleId));
        ArrayList<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
        for (Long privilegeId : rolePrivilegesParam.getPrivilegeIds()) {
            SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
            sysRolePrivilege.setPrivilegeId(privilegeId);
            sysRolePrivilege.setRoleId(roleId);
            sysRolePrivileges.add(sysRolePrivilege);
        }
        boolean b = saveBatch(sysRolePrivileges);
        return b;
    }
}




